package com.gxa.controller;

import com.gxa.pojo.Administrators;
import com.gxa.pojo.Log;
import com.gxa.service.AdministratorsService;
import com.gxa.service.LogService;
import com.gxa.utils.Constants;
import com.gxa.utils.ResponseData;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Administrators)表控制层
 *
 * @author makejava
 * @since 2020-11-26 19:45:15
 */
@RestController
@RequestMapping("administrators")
@Api(value = "权限控制类",tags = "权限控制类")
public class AdministratorsController {
    /**
     * 服务对象
     */
    @Resource
    private AdministratorsService administratorsService;
    @Autowired
    private ResponseData responseData;
    @Autowired
    private LogService logService;
    /**
     * 查询全部管理员
     *
     * @return 全部管理员
     */
    @GetMapping("queryAdministrators")
    @ApiOperation(value = "查询全部管理员",notes = "查询全部管理员",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId",value = "管理员id",paramType = "query",required = true,dataType = "int")
    })
    public ResponseData queryAdministrators(Integer adminId) {
        List<Administrators> administratorsList=administratorsService.queryAdministrators(adminId);
        responseData.setCode(Constants.SUCCESS);
        responseData.setMsg("查询成功");
        responseData.setData(administratorsList);
        return responseData;
    }

    /**
     * 添加管理员
     *
     * @return
     */
    @PostMapping("addAdministrators")
    @ApiOperation(value = "添加管理员",notes = "添加管理员",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=400,message="添加失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData addAdministrators(@RequestBody @ApiParam(name = "administrators",value = "管理员实体",required = true) Administrators administrators) {
        int result=administratorsService.addAdministrators(administrators);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("添加成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("添加失败");
            responseData.setData(null);
        }
        return responseData;
    }
    /**
     * 查询全部权限菜单
     *
     */
    @GetMapping("queryPremiereMenu")
    @ApiOperation(value = "查询全部权限菜单",notes = "查询全部管理员",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData queryPremiereMenu() {
        List<Administrators> administratorsList=administratorsService.queryPremiereMenu();
        responseData.setCode(Constants.SUCCESS);
        responseData.setMsg("查询成功");
        responseData.setData(administratorsList);
        return responseData;
    }
    /**
     * 给管理员添加权限
     *
     */
    @PostMapping("addAdministratorsPremiereMenu")
    @ApiOperation(value = "给管理员添加权限",notes = "给管理员添加权限",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="添加成功"),
            @ApiResponse(code=400,message="添加失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData addAdministratorsPremiereMenu(Administrators administrators) {
        int result=administratorsService.addAdministratorsPremiereMenu(administrators);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("添加成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("添加失败");
            responseData.setData(null);
        }
        return responseData;
    }
    /**
     * 修改管理员
     *
     * @return
     */
    @PostMapping("updateAdministrators")
    @ApiOperation(value = "修改管理员",notes = "修改管理员",httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="修改成功"),
            @ApiResponse(code=400,message="修改失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public ResponseData updateAdministrators(@RequestBody @ApiParam(name = "administrators",value = "管理员实体",required = true) Administrators administrators) {
        int result=administratorsService.updateAdministrators(administrators);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("修改成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("修改失败");
            responseData.setData(null);
        }
        return responseData;
    }

    /**
     * 根据管理员主键查询日志
     *
     * @return 日志集合
     */
    @GetMapping("queryLogByAdminId")
    @ApiOperation(value = "根据管理员主键查询日志",notes = "根据管理员主键查询日志",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId",value = "管理员id",paramType = "query",required = true,dataType = "int")
    })
    public ResponseData queryLogByAdminId(Integer adminId) {
        List<Log> logs=logService.queryLogByAdminId(adminId);
        responseData.setCode(Constants.SUCCESS);
        responseData.setMsg("查询成功");
        responseData.setData(logs);
        return responseData;
    }
    /**
     * 批量删除日志
     *
     */
    @GetMapping("deleteLogByLogIds")
    @ApiOperation(value = "批量删除日志",notes = "批量删除日志",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="删除成功"),
            @ApiResponse(code=400,message="删除失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "logIds", value = "日志ID数组", dataType = "int", paramType = "query", allowMultiple = true, required = true )
    })
    public ResponseData deleteLogByLogIds(Integer[] logIds) {
        int result=logService.deleteLogByLogIds(logIds);
        if (result>0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("删除成功");
            responseData.setData(null);
        }else {
            responseData.setCode(Constants.FAILED);
            responseData.setMsg("删除失败");
            responseData.setData(null);
        }
        return responseData;
    }
    /**
     * 模糊查询日志
     *
     */
    @GetMapping("selectLogByOperator")
    @ApiOperation(value = "模糊查询日志",notes = "模糊查询日志",httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code=200,message="查询成功"),
            @ApiResponse(code=400,message="查询失败"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "operator", value = "操作者", dataType = "String", paramType = "query",required = true )
    })
    public ResponseData selectLogByOperator(String operator) {
        List<Log> logs=logService.selectLogByOperator(operator);
        responseData.setCode(Constants.SUCCESS);
        responseData.setMsg("查询成功");
        responseData.setData(logs);
        return responseData;
    }
}